package io.lkt.modules.sys.controller;

import com.google.common.collect.Lists;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BMessageKefuEntity;
import io.lkt.modules.sys.entity.BPosterInfoEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.BMessageKefuService;
import io.lkt.modules.sys.service.SysGzhService;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 公众号信息
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-21 14:48:00
 */
@RestController
@RequestMapping("/sys/sysgzh")
public class SysGzhController extends AbstractController{
    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private BMessageKefuService bMessageKefuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:sysgzh:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysGzhService.queryPage(params);

        List<?> list = page.getList();
        if (CollectionUtils.isNotEmpty(list)) {
            List<SysGzhEntity> sysGzhEntityList = Lists.newArrayList();

            for (Object temp : list) {
                SysGzhEntity sysGzhEntity = (SysGzhEntity) temp;
                sysGzhEntityList.add(sysGzhEntity);
            }

            // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
            List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
            sysGzhEntityList = sysGzhEntityList.stream().filter(SysGzhEntity -> gzhCodeList.contains(SysGzhEntity.getGzhCode())).collect(Collectors.toList());

            page.setList(sysGzhEntityList);
            page.setTotalCount(sysGzhEntityList.size());
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{gzhCode}")
//    @RequiresPermissions("sys:sysgzh:info")
    public R info(@PathVariable("gzhCode") String gzhCode) {
        SysGzhEntity sysGzh = sysGzhService.getById(gzhCode);
        return R.ok().put("sysGzh", sysGzh);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:sysgzh:save")
    public R save(@RequestBody SysGzhEntity sysGzh) {
        sysGzhService.save(sysGzh);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:sysgzh:update")
    public R update(@RequestBody SysGzhEntity sysGzh) {
        ValidatorUtils.validateEntity(sysGzh);
        sysGzhService.updateById(sysGzh);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:sysgzh:delete")
    public R delete(@RequestBody String[] gzhCodes) {
        sysGzhService.removeByIds(Arrays.asList(gzhCodes));

        return R.ok();
    }

}
